/**
 * 
 */
package com.surelution.cppei.weixin.gate

import com.surelution.cppei.weixin.gate.QrAssign;
import com.surelution.cppei.weixin.gate.SubscriberSubAccount;

/**
 * @author <a href="mailto:guangzong.syu@gmail.com">guangzong</a>
 *
 */
class ScanAction extends CppeiBaseAction {

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#accept()
	 */
	@Override
	public boolean accept() {
		getParam("Event") == "SCAN"
	}

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#execute()
	 */
	@Override
	public void execute() {
		String eventKey = getParam("EventKey")
		if(eventKey) {
			def code = eventKey as Integer
			def subAccount = QrAssign.findByQrscene(code)
			if(subAccount) {
				def ssa = SubscriberSubAccount.findAllBySubAccountAndSubscriber(subAccount, subscriber)
				if(!ssa) {
					ssa = new SubscriberSubAccount(subAccount:subAccount, subscriber:subscriber, userid:UUID.randomUUID().toString())
					ssa.save(flush:true)
					subscriber.subAccount = subAccount
					subscriber.save(flush:true)
				}
			}
		}
		
		if(subscriber.subAccount) {
			//TODO inform the sub account
		}
	}

}
